package demo;

import java.util.logging.Logger;
import java.util.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import com.google.appengine.api.datastore.Transaction;

public class UserAO {

	private static final Logger log = Logger.getLogger(MyUserServlet.class
			.getName());
	private static final Logger logg = Logger.getLogger(UserServlet.class
			.getName());
	EntityManager em;

	public UserAO() {

	}

	public boolean add(String trackername, String password, String simno,
			String imei, String typeoftracker, String phoneno, String passcode) {
		em = EMF.get().createEntityManager();
		User user = new User();

		user.setTrackername(trackername);
		user.setPassword(password);
		user.setSimno(simno);
		user.setImei(imei);
		user.setTypeoftracker(typeoftracker);
		user.setPhoneno(phoneno);
		user.setPasscode(passcode);
		user.setRole("caregiver");
		/*
		 * user.setTrackername("tracker"); user.setPhoneno("97284339");
		 * user.setImei("354037050138621");
		 */
		em.persist(user);
		em.close();

		return true;
	}

	public List<User> getAllImei() {
		List<User> imeilist = new ArrayList<User>();
		em = EMF.get().createEntityManager();
		Query q = em.createQuery("select imei from User u");
		imeilist = q.getResultList();
		em.close();
		return imeilist;
	}

	public List<User> getAllphoneno() {
		List<User> list = new ArrayList<User>();
		em = EMF.get().createEntityManager();
		Query q = em.createQuery("select phoneno from User u");
		list = q.getResultList();
		em.close();
		return list;
	}

	public String getaPasscode(String phoneno) {
		String passcode = "";
		em = EMF.get().createEntityManager();
		Query q = em.createQuery("select passcode from User u where phoneno='"
				+ phoneno + "'");
		passcode = q.getSingleResult().toString();
		em.close();
		return passcode;
	}

	public String getAuthentication(String phoneno, String passcode) {
		String role = "";
		String imei = "";

		em = EMF.get().createEntityManager();

		Query q = em.createQuery("select imei from User u where phoneno='"
				+ phoneno + "' AND passcode='" + passcode + "'");
		imei = q.getSingleResult().toString();

		Query m = em.createQuery("select role from User u where phoneno='"
				+ phoneno + "' AND passcode='" + passcode + "'");
		role = m.getSingleResult().toString();

		em.close();

		String authentication = imei + "," + role;
		return authentication;
	}

	// get last 4 digit of imei number for passcode
	public String getLastnCharacters(String inputString) {
		int length = inputString.length();
		if (length <= 4) {
			return inputString;
		}
		int startIndex = length - 4;
		return inputString.substring(startIndex);
	}

	// check for alphabet in phone number, sim number, imei 
	public final boolean containsDigit(final String s) {
		boolean containsDigit = false;

		if (s != null) {
			for (char c : s.toCharArray()) {
				if (Character.isDigit(c)) {
					containsDigit = true;
					continue;
				} else {
					containsDigit = false;
					break;
				}
			}
		}

		return containsDigit;
	}
	
	
	// check for valid sim number (for sending command to tracker) --> should start with 9 or 8 for sgp  
		public final boolean validSim(final String s) {
			boolean containsDigit = false;

			if (s != null) {
				char firstdigit = s.charAt(0);
				if (firstdigit == '9' || firstdigit == '8')
				{
					containsDigit = true;
				}
				else
				{
					containsDigit = false;
				}
			}

			return containsDigit;
		}
		
		// check for valid phone number (care giver number) --> should start with 9 or 8 or 6 
				public final boolean validphone(final String s) {
					boolean containsDigit = false;

					if (s != null) {
						char firstdigit = s.charAt(0);
						if (firstdigit == '9' || firstdigit == '8' || firstdigit == '6')
						{
							containsDigit = true;
						}
						else
						{
							containsDigit = false;
						}
					}

					return containsDigit;
				}
	

	public List<User> getuserData() {
		List<User> list = new ArrayList<User>();
		em = EMF.get().createEntityManager();
		Query q = em.createQuery("select from User u");
		list = q.getResultList();
		em.close();
		if(list.isEmpty() == false)
		return list;
		return list;
		
	}

	public boolean deleteUser(long uid) {

		em = EMF.get().createEntityManager();

		EntityTransaction tx = em.getTransaction();

		tx.begin();
		try {
			try {
				User delete = em.find(User.class, uid);
				em.remove(delete);
				tx.commit();
			} catch (Exception e) {
			}
		} finally {
			em.close();
		}

		return true;
	}
	
	public boolean updateUser(long uid, String trackername, String password, String simno, String imei, String typeoftracker, String phoneno, String passcode, String role) {

		em = EMF.get().createEntityManager();

		EntityTransaction tx = em.getTransaction();

		tx.begin();
		try {
			try {
				User update = em.find(User.class, uid);
				update.setTrackername(trackername);
				update.setPassword(password);
				update.setSimno(simno);
				update.setImei(imei);
				update.setTypeoftracker(typeoftracker);
				update.setPhoneno(phoneno);
				update.setPasscode(passcode);
				update.setRole(role);
				tx.commit();
			} catch (Exception e) {
			}
		} finally {
			em.close();
		}

		return true;
	}

}
